<?php
/**
 * Created by PhpStorm.
 * User: yuyj
 * Date: 16-12-23
 * Time: 下午3:43
 */

namespace mybackend\controllers\common;


use common\services\myhtsq\ErpUserService;
use mybackend\controllers\BaseController;
use yii\filters\AccessControl;

class ErpUserController extends BaseController
{
    protected $service;

    public function __construct($id, $module, ErpUserService $service, $config = [])
    {
        $this->service = $service;
        parent::__construct($id, $module, $config);
    }

    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }

    /*
     * Author yuyj
     * Date 2016-12-30
     * @apiVersion 1.0.0
     * @api {get} /api/index/common/erp-user/get-user-by-id 获取用户信息
     * @apiGroup erp-user-controller
     *
     * @apiParam query {string{..36}} [id] 用户的guid
     *
     * @apiResponse 200 {
     *"retCode": 0,
     *"errMsg": "",
     *"data": {
     *"UserGUID": "814eddb3-832e-e511-9b71-848f69dfab18",
     *"UserCode": "yuyj",
     *"UserName": "余云建",
     *"BUGUID": "68af85a3-c5ee-4626-a00e-3a7efe8a86cc",
     *"Email": "yuyj@mysoft.com.cn"
     *}
     *}
     */
    public function actionGetUserById($id)
    {
        $data = $this->service->getUserById($id);

        return $this->exportJson(
            $data,
            $this->service->getErrCode(),
            $this->service->getErrMsg()
        );
    }

    /*
    *
    * Author yuyj
    * Date 2017-01-03
    * @apiVersion 1.0.0
    * @api {get} /api/index/common/erp-user/get-area-charge-list 获取区域负责人列表
    * @apiGroup erp-user-controller
    *
    * @apiParam query {string} $deliverUserId 交付人员ID
    *
    * @apiResponse 200 {
    * "data": [
    *    {
    *           "UserGUID": "cccdb74e-96ea-e511-bca5-848f69dfab18",
    *           "UserName": "江富炳",
    *           "BUGUID": "68af85a3-c5ee-4626-a00e-3a7efe8a86cc",
    *           "DepartmentGUID": "",
    *           "BUName": "深圳市明源云客电子商务有限公司",
    *           "DepartmentName": "研发部"
    *       }
    *   ],
    * "retCode": "0",
    * "errMsg": ""
    *}
    */
    public function actionGetAreaChargeList($deliverUserId)
    {
        $user = $this->service->getUserById($deliverUserId);

        if (empty($user)) {
            $this->exportJson(null, -1, '该交付用户不存在');
        }

        $data = $this->service->getAreaChargeList($user['BUGUID']);

        return $this->exportJson($data, $this->service->getErrCode(), $this->service->getErrMsg());
    }

    /*
    *
    * Author yuyj
    * Date 2017-01-03
    * @apiVersion 1.0.0
    * @api {get} /api/index/common/erp-user/get-finance-charge-list 获取财务负责人列表
    * @apiGroup erp-user-controller
    *
    * @apiResponse 200 {
    * "data": [
    *    {
    *           "UserGUID": "cccdb74e-96ea-e511-bca5-848f69dfab18",
    *           "UserName": "江富炳",
    *           "BUGUID": "68af85a3-c5ee-4626-a00e-3a7efe8a86cc",
    *           "DepartmentGUID": "",
    *           "BUName": "深圳市明源云客电子商务有限公司",
    *           "DepartmentName": "研发部"
    *       }
    *   ],
    * "retCode": "0",
    * "errMsg": ""
    *}
    */
    public function actionGetFinanceChargeList()
    {
        $data = $this->service->getFinanceChargeList();

        return $this->exportJson($data, $this->service->getErrCode(), $this->service->getErrMsg());
    }
}